package cn.gson.school.controller.jiaoxue;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import com.github.pagehelper.*;

import cn.gson.school.dao.jiaoxue.ZhangJieDao;
import cn.gson.school.dao.xitong.UserInfoDao;
import cn.gson.school.entity.jiaoxue.*;
import cn.gson.school.mappers.jiaoxue.ZhangJieMappers;

@Controller
@RequestMapping("/zhangjie")
public class ZhangJieController {

	@Autowired
	private ZhangJieMappers zhangJieMappers;

	@Autowired
	private ZhangJieDao zhangjieDao;

	@Autowired
	private UserInfoDao userinfoDao;

	@RequestMapping("/list")
	@ResponseBody
	public Map<String, Object> zhangjieList(ChapterEntity chapterEntity,
			@RequestParam(name = "page", defaultValue = "1") int pageNum,
			@RequestParam(name = "rwos", defaultValue = "10") int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<ChapterEntity> list = zhangJieMappers.zhangjieList(chapterEntity);
		PageInfo<ChapterEntity> info = new PageInfo<>(list);
		Map<String, Object> obj = new HashMap<String, Object>();
		obj.put("total", info.getTotal());
		obj.put("rows", info.getList());
		return obj;
	}

	@PostMapping("save")
	@ResponseBody
	@Transactional
	public Map<String, Object> save(@Valid ChapterEntity chapterEntity, HttpServletRequest request, Model model,
			BindingResult br) {
		Map<String, Object> obj = new HashMap<String, Object>();
		obj.put("success", false);
		if (br.hasErrors()) {
			obj.put("message", "校验失败！");
			obj.put("errors", br.getFieldErrors());
		} else {
			// 获取隐藏域章节编号
			String id = request.getParameter("zhangJieId");
			String userId = request.getParameter("userinfo");// 获取操作人编号
			String jieDuan = request.getParameter("jieDuan");// 获取选中的所属阶段
			String zhangJieName = request.getParameter("zhangJieName");// 获取章节名称
			ChapterEntity chapter;
			if (id == null || "".equals(id)) {
				// 添加
				System.out.println("添加");
				chapter = new ChapterEntity();
			} else {
				// 编辑
				System.out.println("编辑");
				chapter = zhangjieDao.findOne(Long.valueOf(id));
				chapter.setChapterId(Long.valueOf(id));
			}
			chapter.setPhaseId(Long.valueOf(jieDuan));
			chapter.setChapterName(zhangJieName);
			chapter.setAddingTime(new Date());
			chapter.setDelete(1);
			chapter.setUserinfoId(userinfoDao.findOne(Long.valueOf(userId)));
			zhangjieDao.save(chapter);
			obj.put("success", true);
		}
		return obj;
	}

	@GetMapping("delete/{id}")
	@ResponseBody
	// 指定使用jpa的事务管理器
	@Transactional
	public Map<String, Object> delete(@PathVariable("id") Long id) {
		Map<String, Object> obj = new HashMap<>();
		obj.put("success", false);
		ChapterEntity chapterEntity;
		if (id != null && (chapterEntity= zhangjieDao.findOne(id)) != null) {
			chapterEntity= zhangjieDao.findOne(id);
			chapterEntity.setDelete(0);
			zhangjieDao.save(chapterEntity);
			obj.put("success", true);
		} else {
			obj.put("message", "你要删除的章节不存在！");
		}
		return obj;
	}
}
